﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using Pharmacy.Entities;
using Pharmacy.Data;

#endregion

namespace Pharmacy.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="GrnDetailsProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class GrnDetailsProviderBaseCore : EntityProviderBase<Pharmacy.Entities.GrnDetails, Pharmacy.Entities.GrnDetailsKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, Pharmacy.Entities.GrnDetailsKey key)
		{
			return Delete(transactionManager, key.GrnDetailId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_grnDetailId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int64 _grnDetailId)
		{
			return Delete(null, _grnDetailId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_grnDetailId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int64 _grnDetailId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_GRNDetails_DrugDispo key.
		///		FK_GRNDetails_DrugDispo Description: 
		/// </summary>
		/// <param name="_drugId"></param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.GrnDetails objects.</returns>
		public TList<GrnDetails> GetByDrugId(System.String _drugId)
		{
			int count = -1;
			return GetByDrugId(_drugId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_GRNDetails_DrugDispo key.
		///		FK_GRNDetails_DrugDispo Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_drugId"></param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.GrnDetails objects.</returns>
		/// <remarks></remarks>
		public TList<GrnDetails> GetByDrugId(TransactionManager transactionManager, System.String _drugId)
		{
			int count = -1;
			return GetByDrugId(transactionManager, _drugId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_GRNDetails_DrugDispo key.
		///		FK_GRNDetails_DrugDispo Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_drugId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.GrnDetails objects.</returns>
		public TList<GrnDetails> GetByDrugId(TransactionManager transactionManager, System.String _drugId, int start, int pageLength)
		{
			int count = -1;
			return GetByDrugId(transactionManager, _drugId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_GRNDetails_DrugDispo key.
		///		fkGrnDetailsDrugDispo Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_drugId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.GrnDetails objects.</returns>
		public TList<GrnDetails> GetByDrugId(System.String _drugId, int start, int pageLength)
		{
			int count =  -1;
			return GetByDrugId(null, _drugId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_GRNDetails_DrugDispo key.
		///		fkGrnDetailsDrugDispo Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_drugId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.GrnDetails objects.</returns>
		public TList<GrnDetails> GetByDrugId(System.String _drugId, int start, int pageLength,out int count)
		{
			return GetByDrugId(null, _drugId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_GRNDetails_DrugDispo key.
		///		FK_GRNDetails_DrugDispo Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_drugId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.GrnDetails objects.</returns>
		public abstract TList<GrnDetails> GetByDrugId(TransactionManager transactionManager, System.String _drugId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_GRNDetails_GRN2 key.
		///		FK_GRNDetails_GRN2 Description: 
		/// </summary>
		/// <param name="_grnId"></param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.GrnDetails objects.</returns>
		public TList<GrnDetails> GetByGrnId(System.String _grnId)
		{
			int count = -1;
			return GetByGrnId(_grnId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_GRNDetails_GRN2 key.
		///		FK_GRNDetails_GRN2 Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_grnId"></param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.GrnDetails objects.</returns>
		/// <remarks></remarks>
		public TList<GrnDetails> GetByGrnId(TransactionManager transactionManager, System.String _grnId)
		{
			int count = -1;
			return GetByGrnId(transactionManager, _grnId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_GRNDetails_GRN2 key.
		///		FK_GRNDetails_GRN2 Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_grnId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.GrnDetails objects.</returns>
		public TList<GrnDetails> GetByGrnId(TransactionManager transactionManager, System.String _grnId, int start, int pageLength)
		{
			int count = -1;
			return GetByGrnId(transactionManager, _grnId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_GRNDetails_GRN2 key.
		///		fkGrnDetailsGrn2 Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_grnId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.GrnDetails objects.</returns>
		public TList<GrnDetails> GetByGrnId(System.String _grnId, int start, int pageLength)
		{
			int count =  -1;
			return GetByGrnId(null, _grnId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_GRNDetails_GRN2 key.
		///		fkGrnDetailsGrn2 Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_grnId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.GrnDetails objects.</returns>
		public TList<GrnDetails> GetByGrnId(System.String _grnId, int start, int pageLength,out int count)
		{
			return GetByGrnId(null, _grnId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_GRNDetails_GRN2 key.
		///		FK_GRNDetails_GRN2 Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_grnId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.GrnDetails objects.</returns>
		public abstract TList<GrnDetails> GetByGrnId(TransactionManager transactionManager, System.String _grnId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override Pharmacy.Entities.GrnDetails Get(TransactionManager transactionManager, Pharmacy.Entities.GrnDetailsKey key, int start, int pageLength)
		{
			return GetByGrnDetailId(transactionManager, key.GrnDetailId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_GRNDetails index.
		/// </summary>
		/// <param name="_grnDetailId"></param>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.GrnDetails"/> class.</returns>
		public Pharmacy.Entities.GrnDetails GetByGrnDetailId(System.Int64 _grnDetailId)
		{
			int count = -1;
			return GetByGrnDetailId(null,_grnDetailId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_GRNDetails index.
		/// </summary>
		/// <param name="_grnDetailId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.GrnDetails"/> class.</returns>
		public Pharmacy.Entities.GrnDetails GetByGrnDetailId(System.Int64 _grnDetailId, int start, int pageLength)
		{
			int count = -1;
			return GetByGrnDetailId(null, _grnDetailId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_GRNDetails index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_grnDetailId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.GrnDetails"/> class.</returns>
		public Pharmacy.Entities.GrnDetails GetByGrnDetailId(TransactionManager transactionManager, System.Int64 _grnDetailId)
		{
			int count = -1;
			return GetByGrnDetailId(transactionManager, _grnDetailId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_GRNDetails index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_grnDetailId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.GrnDetails"/> class.</returns>
		public Pharmacy.Entities.GrnDetails GetByGrnDetailId(TransactionManager transactionManager, System.Int64 _grnDetailId, int start, int pageLength)
		{
			int count = -1;
			return GetByGrnDetailId(transactionManager, _grnDetailId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_GRNDetails index.
		/// </summary>
		/// <param name="_grnDetailId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.GrnDetails"/> class.</returns>
		public Pharmacy.Entities.GrnDetails GetByGrnDetailId(System.Int64 _grnDetailId, int start, int pageLength, out int count)
		{
			return GetByGrnDetailId(null, _grnDetailId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_GRNDetails index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_grnDetailId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.GrnDetails"/> class.</returns>
		public abstract Pharmacy.Entities.GrnDetails GetByGrnDetailId(TransactionManager transactionManager, System.Int64 _grnDetailId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#region _GRNDetails_AutoQuantity 
		
		/// <summary>
		///	This method wrap the '_GRNDetails_AutoQuantity' stored procedure. 
		/// </summary>
		/// <param name="drugId"> A <c>System.String</c> instance.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="TList&lt;GrnDetails&gt;"/> instance.</returns>
		public TList<GrnDetails> AutoQuantity(System.String drugId)
		{
			return AutoQuantity(null, 0, int.MaxValue , drugId);
		}
		
		/// <summary>
		///	This method wrap the '_GRNDetails_AutoQuantity' stored procedure. 
		/// </summary>
		/// <param name="drugId"> A <c>System.String</c> instance.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="TList&lt;GrnDetails&gt;"/> instance.</returns>
		public TList<GrnDetails> AutoQuantity(int start, int pageLength, System.String drugId)
		{
			return AutoQuantity(null, start, pageLength , drugId);
		}
				
		/// <summary>
		///	This method wrap the '_GRNDetails_AutoQuantity' stored procedure. 
		/// </summary>
		/// <param name="drugId"> A <c>System.String</c> instance.</param>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="TList&lt;GrnDetails&gt;"/> instance.</returns>
		public TList<GrnDetails> AutoQuantity(TransactionManager transactionManager, System.String drugId)
		{
			return AutoQuantity(transactionManager, 0, int.MaxValue , drugId);
		}
		
		/// <summary>
		///	This method wrap the '_GRNDetails_AutoQuantity' stored procedure. 
		/// </summary>
		/// <param name="drugId"> A <c>System.String</c> instance.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="TList&lt;GrnDetails&gt;"/> instance.</returns>
		public abstract TList<GrnDetails> AutoQuantity(TransactionManager transactionManager, int start, int pageLength , System.String drugId);
		
		#endregion
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;GrnDetails&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;GrnDetails&gt;"/></returns>
		public static TList<GrnDetails> Fill(IDataReader reader, TList<GrnDetails> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				Pharmacy.Entities.GrnDetails c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("GrnDetails")
					.Append("|").Append((System.Int64)reader[((int)GrnDetailsColumn.GrnDetailId - 1)]).ToString();
					c = EntityManager.LocateOrCreate<GrnDetails>(
					key.ToString(), // EntityTrackingKey
					"GrnDetails",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new Pharmacy.Entities.GrnDetails();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.GrnDetailId = (System.Int64)reader[((int)GrnDetailsColumn.GrnDetailId - 1)];
					c.GrnId = (System.String)reader[((int)GrnDetailsColumn.GrnId - 1)];
					c.DrugId = (System.String)reader[((int)GrnDetailsColumn.DrugId - 1)];
					c.Quantity = (System.Int32)reader[((int)GrnDetailsColumn.Quantity - 1)];
					c.PoQuantity = (System.Int32)reader[((int)GrnDetailsColumn.PoQuantity - 1)];
					c.UnitPrice = (System.Decimal)reader[((int)GrnDetailsColumn.UnitPrice - 1)];
					c.ExpDate = (reader.IsDBNull(((int)GrnDetailsColumn.ExpDate - 1)))?null:(System.DateTime?)reader[((int)GrnDetailsColumn.ExpDate - 1)];
					c.BatchLot = (reader.IsDBNull(((int)GrnDetailsColumn.BatchLot - 1)))?null:(System.String)reader[((int)GrnDetailsColumn.BatchLot - 1)];
					c.Remark = (reader.IsDBNull(((int)GrnDetailsColumn.Remark - 1)))?null:(System.String)reader[((int)GrnDetailsColumn.Remark - 1)];
					c.OpeningBalance = (System.Int32)reader[((int)GrnDetailsColumn.OpeningBalance - 1)];
					c.OpeningAmount = (System.Decimal)reader[((int)GrnDetailsColumn.OpeningAmount - 1)];
					c.IsComplete = (System.Boolean)reader[((int)GrnDetailsColumn.IsComplete - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="Pharmacy.Entities.GrnDetails"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="Pharmacy.Entities.GrnDetails"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, Pharmacy.Entities.GrnDetails entity)
		{
			if (!reader.Read()) return;
			
			entity.GrnDetailId = (System.Int64)reader[((int)GrnDetailsColumn.GrnDetailId - 1)];
			entity.GrnId = (System.String)reader[((int)GrnDetailsColumn.GrnId - 1)];
			entity.DrugId = (System.String)reader[((int)GrnDetailsColumn.DrugId - 1)];
			entity.Quantity = (System.Int32)reader[((int)GrnDetailsColumn.Quantity - 1)];
			entity.PoQuantity = (System.Int32)reader[((int)GrnDetailsColumn.PoQuantity - 1)];
			entity.UnitPrice = (System.Decimal)reader[((int)GrnDetailsColumn.UnitPrice - 1)];
			entity.ExpDate = (reader.IsDBNull(((int)GrnDetailsColumn.ExpDate - 1)))?null:(System.DateTime?)reader[((int)GrnDetailsColumn.ExpDate - 1)];
			entity.BatchLot = (reader.IsDBNull(((int)GrnDetailsColumn.BatchLot - 1)))?null:(System.String)reader[((int)GrnDetailsColumn.BatchLot - 1)];
			entity.Remark = (reader.IsDBNull(((int)GrnDetailsColumn.Remark - 1)))?null:(System.String)reader[((int)GrnDetailsColumn.Remark - 1)];
			entity.OpeningBalance = (System.Int32)reader[((int)GrnDetailsColumn.OpeningBalance - 1)];
			entity.OpeningAmount = (System.Decimal)reader[((int)GrnDetailsColumn.OpeningAmount - 1)];
			entity.IsComplete = (System.Boolean)reader[((int)GrnDetailsColumn.IsComplete - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="Pharmacy.Entities.GrnDetails"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="Pharmacy.Entities.GrnDetails"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, Pharmacy.Entities.GrnDetails entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.GrnDetailId = (System.Int64)dataRow["GRNDetailId"];
			entity.GrnId = (System.String)dataRow["GRNId"];
			entity.DrugId = (System.String)dataRow["DrugId"];
			entity.Quantity = (System.Int32)dataRow["Quantity"];
			entity.PoQuantity = (System.Int32)dataRow["POQuantity"];
			entity.UnitPrice = (System.Decimal)dataRow["UnitPrice"];
			entity.ExpDate = Convert.IsDBNull(dataRow["ExpDate"]) ? null : (System.DateTime?)dataRow["ExpDate"];
			entity.BatchLot = Convert.IsDBNull(dataRow["BatchLot"]) ? null : (System.String)dataRow["BatchLot"];
			entity.Remark = Convert.IsDBNull(dataRow["Remark"]) ? null : (System.String)dataRow["Remark"];
			entity.OpeningBalance = (System.Int32)dataRow["OpeningBalance"];
			entity.OpeningAmount = (System.Decimal)dataRow["OpeningAmount"];
			entity.IsComplete = (System.Boolean)dataRow["IsComplete"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="Pharmacy.Entities.GrnDetails"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">Pharmacy.Entities.GrnDetails Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, Pharmacy.Entities.GrnDetails entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region DrugIdSource	
			if (CanDeepLoad(entity, "DrugDispo|DrugIdSource", deepLoadType, innerList) 
				&& entity.DrugIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.DrugId;
				DrugDispo tmpEntity = EntityManager.LocateEntity<DrugDispo>(EntityLocator.ConstructKeyFromPkItems(typeof(DrugDispo), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.DrugIdSource = tmpEntity;
				else
					entity.DrugIdSource = DataRepository.DrugDispoProvider.GetByDrugId(transactionManager, entity.DrugId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'DrugIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.DrugIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.DrugDispoProvider.DeepLoad(transactionManager, entity.DrugIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion DrugIdSource

			#region GrnIdSource	
			if (CanDeepLoad(entity, "Grn|GrnIdSource", deepLoadType, innerList) 
				&& entity.GrnIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.GrnId;
				Grn tmpEntity = EntityManager.LocateEntity<Grn>(EntityLocator.ConstructKeyFromPkItems(typeof(Grn), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.GrnIdSource = tmpEntity;
				else
					entity.GrnIdSource = DataRepository.GrnProvider.GetByGrnId(transactionManager, entity.GrnId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'GrnIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.GrnIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.GrnProvider.DeepLoad(transactionManager, entity.GrnIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion GrnIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the Pharmacy.Entities.GrnDetails object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">Pharmacy.Entities.GrnDetails instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">Pharmacy.Entities.GrnDetails Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, Pharmacy.Entities.GrnDetails entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region DrugIdSource
			if (CanDeepSave(entity, "DrugDispo|DrugIdSource", deepSaveType, innerList) 
				&& entity.DrugIdSource != null)
			{
				DataRepository.DrugDispoProvider.Save(transactionManager, entity.DrugIdSource);
				entity.DrugId = entity.DrugIdSource.DrugId;
			}
			#endregion 
			
			#region GrnIdSource
			if (CanDeepSave(entity, "Grn|GrnIdSource", deepSaveType, innerList) 
				&& entity.GrnIdSource != null)
			{
				DataRepository.GrnProvider.Save(transactionManager, entity.GrnIdSource);
				entity.GrnId = entity.GrnIdSource.GrnId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region GrnDetailsChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>Pharmacy.Entities.GrnDetails</c>
	///</summary>
	public enum GrnDetailsChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>DrugDispo</c> at DrugIdSource
		///</summary>
		[ChildEntityType(typeof(DrugDispo))]
		DrugDispo,
			
		///<summary>
		/// Composite Property for <c>Grn</c> at GrnIdSource
		///</summary>
		[ChildEntityType(typeof(Grn))]
		Grn,
		}
	
	#endregion GrnDetailsChildEntityTypes
	
	#region GrnDetailsFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;GrnDetailsColumn&gt;"/> class
	/// that is used exclusively with a <see cref="GrnDetails"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class GrnDetailsFilterBuilder : SqlFilterBuilder<GrnDetailsColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the GrnDetailsFilterBuilder class.
		/// </summary>
		public GrnDetailsFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the GrnDetailsFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public GrnDetailsFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the GrnDetailsFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public GrnDetailsFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion GrnDetailsFilterBuilder
	
	#region GrnDetailsParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;GrnDetailsColumn&gt;"/> class
	/// that is used exclusively with a <see cref="GrnDetails"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class GrnDetailsParameterBuilder : ParameterizedSqlFilterBuilder<GrnDetailsColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the GrnDetailsParameterBuilder class.
		/// </summary>
		public GrnDetailsParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the GrnDetailsParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public GrnDetailsParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the GrnDetailsParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public GrnDetailsParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion GrnDetailsParameterBuilder
	
	#region GrnDetailsSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;GrnDetailsColumn&gt;"/> class
	/// that is used exclusively with a <see cref="GrnDetails"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class GrnDetailsSortBuilder : SqlSortBuilder<GrnDetailsColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the GrnDetailsSqlSortBuilder class.
		/// </summary>
		public GrnDetailsSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion GrnDetailsSortBuilder
	
} // end namespace
